namespace map2D {
	/**地砖类，用于渲染场景地图地砖*/
	export class MapBricker {
		private _sceneLay:GYSprite;				
		private _sceneW:number;
		private _sceneH:number;
		private _bricks:Brick[];
		public owner:SceneManagerBase;		
		private _texDict:any;
		public constructor(owner:SceneManagerBase) {
			let s = this;
			s._bricks = [];
			s.owner = owner;
			s._sceneLay = owner.sceneLayer;												
			s._texDict = {};
		}
		public setSceneSize(w:number,h:number):void
		{
			let s = this;
			s._sceneW = w;
			s._sceneH = h;
		}
		public clear():void
		{
			let s = this;				
			for(var value of s._bricks)			
				value.destroy();
			s._bricks.length = 0;
			s._sceneLay.graphics.clear();			
			s._texDict = {};
		}
		public brickCheck():void
		{			
			let s = this;		
			let len:number;		
			len = s._bricks.length;
			while(--len>-1)
			{
				if(s._bricks[len].isOutScreen())
				{
					s._bricks[len].destroy();
					s._bricks.splice(len, 1);
				}					
			}			
		}
		
		// public static test:number=0;
		public brickDraw(x:number,y:number,absX:number, absY:number, innerCol:number,innerRow:number,mapCol:number,mapRow:number):void
		{
			let s = this;			
			let brick:Brick = s.findBrick(`${absX}_${absY}`)
			if(brick)
			{
				brick.x = x;
				brick.y = y;
				return;
			}
			brick = new Brick(s, absX, absY, x, y, innerCol, innerRow, mapCol, mapRow);			
			s._bricks.push(brick);
		}
		public draw(tex:Laya.Texture,x:number,y:number,innerCol:number,innerRow:number):void
		{			
			let s = this;
			if(tex == null)
				return;					
			let areaSize:number;
			let tx:number,ty:number,drawX:number,drawY:number,drawW:number,drawH:number;
			let pt:Laya.Point = new Laya.Point;
			areaSize = s.owner.mapAreaSize;
			tx = x;
			ty = y;						
			let g:Laya.Graphics;					
			g = s._sceneLay.graphics;
			
			drawX = x;
			drawY = y;
			drawW = drawH = areaSize;			
			g.drawTexture(tex,drawX,drawY,drawW,drawH);
		}
		// public drawTestBrick(tex:Laya.Texture,brick:Brick)
		// {
		// 	if(brick.mapCol == 0 && brick.mapRow == 1)
		// 	{
		// 		let g:Laya.Graphics = ghost.Module.getInstance().testLay.graphics;
		// 		g.clear();
		// 		// g.drawTexture(tex,brick.x+180,brick.y-399,512,512);
		// 		// g.drawTexture(tex,brick.x+380,brick.y-399,512,512);
		// 		g.fillTexture(tex,brick.x+180,brick.y-399,512,512);
		// 	}			
		// }
		public render():void
		{
			let len:number;
			let s = this;
			s._sceneLay.graphics.clear();
			len = s._bricks.length;
			while(--len>-1)
			{				
				s._bricks[len].draw();
			}
		}
		public findBrick(key:string):Brick
		{
			let len:number;
			let s = this;
			len = s._bricks.length;
			while(--len>-1)
			{
				if(s._bricks[len].key == key)
					return s._bricks[len];
			}
			return null;
		}
		public addTexCount(url:string,tex:Laya.Texture):void
		{
			let s= this;
			if(s._texDict[url] == null)
				s._texDict[url] = {texture:tex,count:0,url:url};
			++s._texDict[url].count;
		}
		public removeTexCount(url:string,tex:Laya.Texture):void
		{
			let s= this;
			if(s._texDict[url])				
			{
				--s._texDict[url].count;
				if(s._texDict[url].count == 0)
					GYLoader.deleteResByKey(url);
			}				
		}
	}	
	export class Brick
	{
		/**世界坐标x*/absX:number;
		/**世界坐标y*/absY:number;
		/**屏幕坐标x*/x:number;
		/**屏幕坐标y*/y:number;
		/**图集内列索引*/innerCol:number;
		/**图集内行索引*/innerRow:number;
		/**地图砖块列索引*/mapCol:number;
		/**地图砖块行索引*/mapRow:number;
		/**砖块管理器*/owner:MapBricker;		
		/**地图管理器*/destroyed:boolean;
		/**唯一键*/key:string;
		scene:SceneManagerBase;
		tex:Laya.Texture;
		url:string;
		constructor(o:MapBricker,absX:number, absY:number, x:number,y:number,innerCol:number,innerRow:number,mapCol:number,mapRow:number)
		{
			let s = this;
			s.owner = o;
			s.absX = absX;
			s.absY = absY;
			s.x = x;
			s.y = y;
			s.innerCol = innerCol;
			s.innerRow = innerRow;
			s.mapCol = mapCol;
			s.mapRow = mapRow;
			s.key = `${absX}_${absY}`;						
			s.scene = s.owner.owner;
			s.scene.getBrickTexture(s, mapCol, mapRow);			
		}
		public resLoadComp(loadInfo:LoadInfo):void
		{
			let s = this;	
			if(s.destroyed)		
				return;
			if(s.isOutScreen())
			{
				return;
			}
			if(loadInfo.content && loadInfo.content.res)
			{				
				s.tex = loadInfo.content.res; 
				s.owner.addTexCount(s.url, s.tex);
				s.draw();
			}
		}
		public draw():void
		{
			let s = this;			
			s.owner.draw(s.tex, s.x, s.y, s.innerCol, s.innerRow);
			// s.owner.drawTestBrick(s.tex, s);
		}
		public isOutScreen():boolean
		{
			let s = this;
			if(s.scene.mapX > s.absX + s.scene.mapAreaSize || s.scene.mapX + s.scene.sceneWidth < s.absX)
				return true;
			if(s.scene.mapY > s.absY + s.scene.mapAreaSize || s.scene.mapY + s.scene.sceneHeight < s.absY)
				return true;
		}
		public destroy():void
		{
			let s= this;
			if(s.destroyed)
				return;
			s.destroyed = true;
			s.owner.removeTexCount(s.url, s.tex);
		}
		
	}	
}